﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;
using System.Windows.Forms;
using LogLib;
using System.Drawing;
using KXmlUIControl.KControls;

namespace KXmlUIControl
{
    public class KXmlParser
    {
        public static List<T> GetXmlObject<T>(XElement root, string parentName, string nodeName) where T : new()
        {
            List<T> listOuts = new List<T>();
            XElement parent = root.Element(parentName);
            if (parent == null)
                return listOuts;
            IEnumerable<XElement> elems = parent.Elements(nodeName);
            if (elems == null)
            {
                return listOuts;
            }
            if (elems.Count() == 0)
            {
                return listOuts;
            }         
            try
            {
                Type type = typeof(T);
                foreach (XElement e in elems)
                {
                    T temp = ParseXMLObject<T>(e);                  
                    listOuts.Add(temp);
                }
                return listOuts;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                Log.WriteException(ex.ToString());
                return listOuts;
            }
        }

        public static T GetXmlObject<T>(XElement root, string nodeName) where T : new()
        {
            T t = new T();
            try
            {
                XElement node = root.Element(nodeName);
                if (node == null)
                {
                    return t;
                }              
                return ParseXMLObject<T>(node);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                Log.WriteException(ex.ToString());
                return t;
            }
        }

        public static T ParseXMLObject<T>(XElement e) where T : new()
        {
            Type type = typeof(T);
            T obj = new T();
            IEnumerable<XAttribute> atts = e.Attributes();
            string name = "", value = "";
            PropertyInfo propertyInfo = null;
            foreach (XAttribute att in atts)
            {
                name = att.Name.ToString();
                value = att.Value.Trim();
                propertyInfo = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public);
                if (propertyInfo != null)
                {
                    if (propertyInfo.PropertyType == typeof(string))
                    {
                        propertyInfo.SetValue(obj, value, null); //给对应属性赋值
                    }
                    else if (propertyInfo.PropertyType == typeof(bool) || propertyInfo.PropertyType == typeof(System.Boolean))
                    {
                        propertyInfo.SetValue(obj, StaticTypes.ConvertToBoolean(value, true), null); //给对应属性赋值
                    }
                    else if (propertyInfo.PropertyType == typeof(int))
                    {
                        propertyInfo.SetValue(obj, StaticTypes.ConvertToInt32(value, 120), null); //给对应属性赋值
                    }
                    else if (propertyInfo.PropertyType == typeof(Color))
                    {
                        propertyInfo.SetValue(obj, StaticTypes.GetColor(value), null); //给对应属性赋值(颜色)
                    }
                    else if (propertyInfo.PropertyType == typeof(CheckBoxSize))
                    {
                        propertyInfo.SetValue(obj, value.ToEnum<CheckBoxSize>(), null); //给对应属性赋值(颜色)
                    }
                    else
                    {
                        propertyInfo.SetValue(obj, value, null); //给对应属性赋值
                    }
                }
                else
                {

                }
            }
            return obj;
        }
    }
}
